package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.result.Result;
import com.itheima.health.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
@Api(tags = "检查组相关接口")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 检查组-新增
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("检查组-新增")
    public Result add(@RequestBody CheckGroup checkGroup,@RequestParam(value = "checkitemIds") List<Integer> checkItemIds){
        try {
            log.info("[检查组-新增]data:{} {}",checkGroup,checkItemIds);
            checkGroupService.add(checkGroup,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 检查组-分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    @ApiOperation("检查组-分页查询")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data:{}", queryPageBean);
        //rpc查询数据
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        //封装返回结果
        return new Result(true,MessageConst.ACTION_SUCCESS,pageResult);
    }

    /**
     * 检查组-根据ID查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("检查组-根据ID查询")
    public Result findById(Integer id) {
        CheckGroup checkGroup = null;
        try {
            log.info("[检查组-根据ID查询]id:{}", id);
            checkGroup = checkGroupService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    /**
     *检查组-根据ID查询多个检查项
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("检查组-根据ID查询多个检查项")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> checkItemIds = null;
        try {
            log.info("[检查组-根据ID查询多个检查项id:{}", id);
            checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItemIds);
    }

    /**
     * 检查组-编辑
     * @param checkGroup
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("检查组-编辑")
    public Result edit(@RequestBody CheckGroup checkGroup,@RequestParam(value = "checkitemIds") List<Integer> checkItemIds) {
        log.info("[检查组-编辑]data:{} {}", checkGroup,checkItemIds);
        try {
            checkGroupService.edit(checkGroup,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 检查组-根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCheckGroupitemById")
    @ApiOperation("检查组-根据id删除")
    public Result delete(Integer id){
        log.info("[检查组-根据id删除]id：{}",id);
        try {
            checkGroupService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有检查组
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        log.info("[检查组-查询所有]~");
        List<CheckGroup> checkGroups = null;
        try {
            checkGroups = checkGroupService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }
}
